/**
 *
 */
package steven.common;

/**
 * @author Steven
 *
 */
public class ExtendedRunnable implements Runnable{
	private Runnable task;
	private long interval;
	private boolean stopped;
	private boolean paused;

	public ExtendedRunnable(final long interval){
		this(null, interval);
	}
	public ExtendedRunnable(final Runnable task, final long interval){
		this.task = task;
		this.interval = interval;
		this.stopped = false;
		this.paused = false;
	}
	@Override
	public final void run(){
		while(this.stopped == false){
			if(this.paused == false){
				this.execute();
			}
			try{
				Thread.sleep(this.interval);
			}catch(final InterruptedException e){
			}
		}
	}
	public void execute(){
		if(this.task != null && this.task != this){
			this.task.run();
		}
	}
	public void resume(){
		this.paused = false;
	}
	public void pause(){
		this.paused = true;
	}
	public void stop(){
		this.stopped = true;
	}
	public final Runnable getTask(){
		return this.task;
	}
	public final void setTask(final Runnable task){
		this.task = task;
	}
	public final long getInterval(){
		return this.interval;
	}
	public final void setInterval(final long interval){
		this.interval = interval;
	}
}
